package cn.iocoder.yudao.module.asset.service.manager;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserBindDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.asset.controller.admin.manager.vo.*;
import cn.iocoder.yudao.module.asset.dal.dataobject.manager.ManagerDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.asset.dal.mysql.manager.ManagerMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.asset.enums.ErrorCodeConstants.*;

/**
 * 资产管理 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ManagerServiceImpl implements ManagerService {

    @Resource
    private ManagerMapper managerMapper;

    @Override
    public Integer createManager(ManagerSaveReqVO createReqVO) {
        // 插入
        ManagerDO manager = BeanUtils.toBean(createReqVO, ManagerDO.class);
        managerMapper.insert(manager);
        // 返回
        return manager.getId();
    }

    @Override
    public void updateManager(ManagerSaveReqVO updateReqVO) {
        // 校验存在
        validateManagerExists(updateReqVO.getId());
        // 更新
        ManagerDO updateObj = BeanUtils.toBean(updateReqVO, ManagerDO.class);
        managerMapper.updateById(updateObj);
    }

    @Override
    public void deleteManager(Integer id) {
        // 校验存在
        validateManagerExists(id);
        // 删除
        managerMapper.deleteById(id);
    }

    @Override
        public void deleteManagerListByIds(List<Integer> ids) {
        // 校验存在
        validateManagerExists(ids);
        // 删除
        managerMapper.deleteByIds(ids);
        }

    private void validateManagerExists(List<Integer> ids) {
        List<ManagerDO> list = managerMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(MANAGER_NOT_EXISTS);
        }
    }

    private void validateManagerExists(Integer id) {
        if (managerMapper.selectById(id) == null) {
            throw exception(MANAGER_NOT_EXISTS);
        }
    }

    @Override
    public ManagerDO getManager(Integer id) {
        return managerMapper.selectById(id);
    }

    @Override
    public PageResult<ManagerRespVO> getManagerPage(ManagerPageReqVO pageReqVO) {
        return managerMapper.selectPage(pageReqVO);
    }

    @Override
    @TenantIgnore
    public ManagerRespVO getManagerByBarcodeNumber(String barcodeNumber) {
        ManagerPageReqVO pageReqVO = new ManagerPageReqVO();
        pageReqVO.setBarcodeNumber(barcodeNumber);
        return managerMapper.selectOne(pageReqVO);
    }

}
